﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XingFuStudio.Pioneer.Net;
using XingFuStudio.Pioneer.Entity;
using XingFuStudio.Pioneer.IO;
using FluorineFx.IO;
using XingFuStudio.Pioneer.Applications.City.Config;
using XingFuStudio.Pioneer.Applications.City.Entity;

namespace XingFuStudio.Pioneer.Applications.City.Operation {
    /// <summary>
    /// 分店相关操作
    /// </summary>
    public class Franchise : OPBase{
        /// <summary>
        /// 调用方法的目标类
        /// </summary>
        private readonly string Target = "CCityFranchise.callfunc";

        /// <summary>
        /// 创建业务对象
        /// </summary>
        /// <param name="httpManager"></param>
        /// <param name="account"></param>
        /// <param name="city"></param>
        /// <param name="config"></param>
        public Franchise(HttpManager httpManager, AccountInfo account, CityInfo city, UserConfigSection config)
            : base(httpManager, account, city, config) {
        }

        /// <summary>
        /// 重写操作方法
        /// </summary>
        public override void Operate() {
            base.Operate();

            string message = String.Empty;

            //获取分店列表
            List<ChainStoreInfo> chainStores = this.GetChainStoreList();

            foreach (ChainStoreInfo store in chainStores) {
                try {
                    if (store.CanDeliver) {
                        //上货
                        bool result = this.DeliverChainStore(store, out message);
                        //打印日志
                        this.DoPrintLog(true, message);
                    }
                } catch (Exception ex) {
                    this.DoError(ex);
                }
            }
        }

        /// <summary>
        /// 获取分店列表
        /// </summary>
        /// <returns></returns>
        public List<ChainStoreInfo> GetChainStoreList() {
            List<ChainStoreInfo> stores = new List<ChainStoreInfo>();

            //获取AMF数据
            AMFMessage message = this.GetAMFMessage(this.Target, "getList");

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Dictionary<string, object> data = (Dictionary<string, object>)content["data"];
            object[] list = (object[])data["list"];

            //遍历分店
            foreach (Dictionary<string, object> item in list) {
                try {
                    if ((string)item["type"] == "ok") {
                        object[] states = (object[])item["states"];
                        //遍历
                        foreach (Dictionary<string, object> store in states) {
                            ChainStoreInfo info = new ChainStoreInfo();

                            info.ElementId = Convert.ToInt32((string)item["eid"]);
                            info.FriendId = (string)store["uid"];
                            info.OrderId = (string)store["orderid"];
                            info.CanDeliver = (string)store["type"] == "supply";

                            stores.Add(info);
                        }
                    }
                } catch (Exception ex) {
                    this.DoError(ex);
                }
            }

            return stores;
        }

        /// <summary>
        /// 给分店送货
        /// </summary>
        /// <param name="chainStore">分店信息</param>
        /// <param name="resultMessage">输出参数，用来返回结果信息</param>
        /// <returns></returns>
        public bool DeliverChainStore(ChainStoreInfo chainStore, out string resultMessage) {
            //获取AMF数据
            AMFMessage message = this.GetAMFMessage(this.Target, "deliver",
                chainStore.FriendId, chainStore.OrderId,chainStore.ElementId);

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Dictionary<string, object> data = (Dictionary<string, object>)content["data"];
            //获取收获是否成功的状态
            bool result = (string)data["result"] == "succ";
            if (result) {
                resultMessage = String.Format("供货成功。{0}", ((string)data["msg"]).NoHtml());
            } else {
                resultMessage = String.Format("供货失败。{0}", ((string)data["msg"]).NoHtml());
            }

            return result;
        }

    }
}
